# Maintainer: Renato Silva <br.renatosilva@gmail.com>

_realname='purple-facebook'
_version='66ee77378d82'
_pidgin_revision='bf8ed95aaa1c'
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=20160907.66ee773.bf8ed95
pkgrel=1
pkgdesc='Facebook plugin for libpurple (mingw-w64)'
url='https://github.com/dequis/purple-facebook'
license=(GPL2+)
arch=('any')

options=(!libtool strip staticlibs)
source=("${_realname}-${_version}".tar.gz::"https://github.com/dequis/purple-facebook/archive/${_version}.tar.gz"
        pidgin.tar.gz::"https://bitbucket.org/pidgin/main/get/${_pidgin_revision}.tar.gz"
        001-build-fixes.patch)
sha256sums=('6c4466731955b1adb60896704ff4428f10f15cd8b9e20ac0b27a00c89c9466e2'
            'aeae2dee7255428f9880ea9f7d9d642d5638fccc95cbbbd39ef7d55a0554ab7b'
            'c2e07b9e68b9a4a506dcda5133010246ed2db5337211a65f3e32d5cfd6043399')
depends=(${MINGW_PACKAGE_PREFIX}-libpurple
         ${MINGW_PACKAGE_PREFIX}-json-glib
         ${MINGW_PACKAGE_PREFIX}-glib2
         ${MINGW_PACKAGE_PREFIX}-zlib
         ${MINGW_PACKAGE_PREFIX}-gettext
         ${MINGW_PACKAGE_PREFIX}-gcc-libs)
makedepends=(${MINGW_PACKAGE_PREFIX}-gcc
             ${MINGW_PACKAGE_PREFIX}-pkg-config
             rsync)

pkgver() {
    cd "${srcdir}/${_realname}-${_version}"
    printf '%s.%s.%s' "$(date +%Y%m%d)" "${_version:0:7}" "${_pidgin_revision:0:7}"
}

prepare() {
    # Consolidate sources
    cd "${srcdir}/${_realname}-${_version}"
    while IFS= read -r file; do
        install -D "${srcdir}/pidgin-main-${_pidgin_revision}/${file}" "pidgin/${file}"
    done <<< "$(cat MANIFEST_PIDGIN)"
    touch $(cat MANIFEST_VOIDS)

    # Backport to 2.x
    cd "${srcdir}/${_realname}-${_version}/pidgin"
    for patch in ../patches/*.patch; do
        patch -p1 -i "${patch}"
    done

    cd "${srcdir}/${_realname}-${_version}"
    patch -p1 -i "${srcdir}"/001-build-fixes.patch
    autoreconf -vfi
}

build() {
    msg2 'Synchronizing build directory'
    rsync --recursive --times "${srcdir}/${_realname}-${_version}"/* "${srcdir}/${_realname}-${_version}.build.${CARCH}"
    cd "${srcdir}/${_realname}-${_version}.build.${CARCH}"
    ./configure \
        --prefix=${MINGW_PREFIX} \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --target=${MINGW_CHOST} \
        --with-plugindir=${MINGW_PREFIX}/lib/purple-2

    make
}

package() {
    cd "${srcdir}/${_realname}-${_version}.build.${CARCH}"
    make DESTDIR="${pkgdir}" install
    install -Dm644 "${srcdir}/${_realname}-${_version}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
